package awesome.cpu.structure;

import awesome.cpu.utils.Mode;

public class RegBShifter extends Component {
	
	private static boolean outputDebug = false;
	
	public final static int KEY_REGB = 2;
	private int mRegB;
	public final static int KEY_SHIFT_CNT = 3;
	private int mShiftCnt;
	
	public final static int KEY_RESULT = 4;
	//private int mResult;
	
	private final static RegBShifter mInstance = new RegBShifter();
	
	private RegBShifter() {}
	
	public static RegBShifter getInstance() {
		return mInstance;
	}
	
	@Override
	boolean setData(int keyInterface, int data) {
		switch (keyInterface) {
		case KEY_REGB:
			mRegB = data;
			return true;
		case KEY_SHIFT_CNT:
			mShiftCnt = data;
			return true;
		}
		return false;
	}

	@Override
	int getData(int keyInterface) {
		
		BusMonitor.transferData(InstructionRegister.getInstance(),
				InstructionRegister.KEY_INSTRUCTION_13_9, 
				this, KEY_SHIFT_CNT);
		BusMonitor.transferData(RegisterB.getInstance(), RegisterB.KEY_REGB_OUT,
				this, KEY_REGB);
		
		switch (keyInterface) {
		case KEY_RESULT:
			return shift();
		}
		return 0;
	}

	private int shift() {
		if (outputDebug && Mode.getDebugLevel() >= Mode.DEBUG_LEVEL_PRINT_ALL) {
			System.out.println("RegBShifter: "+mRegB+" << "+mShiftCnt);
		}
		return mRegB << mShiftCnt;
	}
}
